var scene,camera,renderer;
	// 控制元素：旋转，轨道
	var orbitControls,clock;

	(function(){
		init();
        // AddBall()
		// AddBox(0,0);
		// AddModels('./models/gd2.glb', 0.5, 0, 1, 3, 0.4, 1);
		render();
	})()

	// 初始化元素：场景，相机，渲染，控制器，光源
	function init(){
		scene= new THREE.Scene();
		camera = new THREE.PerspectiveCamera(10,window.innerWidth/window.innerHeight, 0.1, 10000);
		camera.position.set(0,500,0);
		renderer = new THREE.WebGLRenderer({ antialias: true,alpha:true });
		renderer.setSize(window.innerWidth,window.innerHeight);
		document.body.appendChild(renderer.domElement);
		orbitControls = new THREE.OrbitControls(camera, renderer.domElement);
		// orbitControls.target = new THREE.Vector3(6.5, 0, -5.5);
		orbitControls.target = new THREE.Vector3(0,0,0);
		clock = new THREE.Clock();
		SetLight();
	}
	// 设置光源：点光源，环境光源
	function SetLight(){
			var point = new THREE.PointLight(0xfffddb, 0.8);
			point.position.set(120, 70, -60);
			scene.add(point);
			var ambient = new THREE.AmbientLight(0xaaaaaa, 0.6);
			scene.add(ambient);

			////背景
			scene.background = new THREE.Color(0xa0a0a0);
			scene.fog = new THREE.Fog(0xa0a0a0, 200, 1000);

			//网格
			const grid = new THREE.GridHelper(2000, 100, 0x000000, 0x000000);
			grid.material.opacity = 0.2;
			grid.material.transparent = true;
			scene.add(grid);
	}
	// 添加控制器并渲染
	function render(){
		var delta = clock.getDelta();
		orbitControls.update(delta);
		requestAnimationFrame(render);
		renderer.render(scene, camera);
	}

	// ******************************添加几何体**********************************
    function AddBall(){
        var geometry = new THREE.SphereGeometry(1,40,40);//半径，球面圆度
        var material = new THREE.MeshLambertMaterial({color:0xff00ff,wireframe:false});
        var mesh = new THREE.Mesh(geometry,material);
        mesh.position.set(0, 0, 0);
        scene.add(mesh);
    }

	function AddBox(x,y){
        y=-y;
		var geometry = new THREE.BoxGeometry(12,0.1,2.8);//x,z,y
		var material = new THREE.MeshLambertMaterial({color:0x666666,wireframe:false});
		var mesh = new THREE.Mesh(geometry,material);
        mesh.position.set(x+6.5,-0.1,-y-1.5);
		scene.add(mesh);
	}

	// 添加glb模型文件
	function AddGround(path,x,y,z){
		var loader = new THREE.GLTFLoader();
		loader.load( path, function ( gltf ) {
			gltf.scene.position.set(x,y,z);
			scene.add( gltf.scene );
		}, undefined, function ( error ) {
			console.error( error );

		} );
	}

	function AddModels(path,x,z,y,width,f,num){

		var ami=0;
		while(ami<num){
			var amj=0;
			while(amj<parseInt(width/f) && ami<num){
				AddGd2(path,x,z,y-amj*f);
				amj++;
				ami++;
			}
			z+=f;
		}
        return z;
	}

	function AddGd2(path,x,z,y){
		var loader = new THREE.GLTFLoader();
		loader.load( path, function ( gltf ) {
			gltf.scene.position.set(x,z,y);
			scene.add( gltf.scene);
		}, undefined, function ( error ) {
			console.error( error );
		} );
	}

	//添加点击事件
	var raycaster = new THREE.Raycaster()
	var mouse = new THREE.Vector2()

	document.body.addEventListener('click',ClickGlb,false);
	function ClickGlb(e){
		mouse.x = (e.clientX / window.innerWidth) * 2 - 1
    	mouse.y = -(e.clientY/window.innerHeight) *2 + 1
		raycaster.setFromCamera( mouse, camera );
		var intersects = raycaster.intersectObjects( scene.children );
		for ( var i = 0; i < intersects.length; i++ ) {
            // console.log(intersects[i].object.position.x-6.5,intersects[i].object.position.z+1.5);
            var obj=intersects[i].object;
            for(var j=0;j<json_list.length;j++){
                if(json_list[j].x==obj.position.x-6.5 && json_list[j].y==obj.position.z+1.5){
                    console.log(json_list[j].name)

                }
            }



			if(obj.material.color.r==1){
				obj.material.color.r=0;
				obj.material.color.g=1
				obj.material.color.b=0;
			}else{
				obj.material.color.r=1;
				obj.material.color.g=1;
				obj.material.color.b=0;
			}

		}
	}

    function FindName(x,y){
        console.log(x,y,json_list.length);
    }
